/*
    i2cdriver.h - I2C driver interface header file
    Copyright (C) 1999-2004  Frodo Looijaard <frodol@dds.nl>, and
                             Mark D. Studebaker <mdsxyz123@yahoo.com>
    Copyright (C) 2004-2012  Jean Delvare <khali@linux-fr.org>
    Copyright (C) 2024  Huawei Technologies Co., Ltd.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301 USA.
*/

#ifndef I2CDRIVER_H
#define I2CDRIVER_H

#include <stdint.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#define DEVICE_NAME "/dev/i2c"

#define I2C_CMD_MAGIC 'i'
#define I2C_CMD_INIT _IOW(I2C_CMD_MAGIC, 0, int)
#define I2C_CMD_WRITE _IOW(I2C_CMD_MAGIC, 1, int)
#define I2C_CMD_READ _IOWR(I2C_CMD_MAGIC, 2, int)


/* Forward declarations */
struct i2c_data_s;

int32_t i2c_open(void);
int32_t i2c_init(int fd, int bus, int addr);
int32_t i2c_close(int fd);
int32_t i2c_read(int fd, struct i2c_data_s *data_in, uint8_t *read_buf);

struct i2c_init_s
{
    uint32_t drv_id;
    uint32_t speed;
    uint32_t is_slave;
    uint32_t address;
    uint32_t sda_hold_ratio;
};

/* 读操作传输参数结构体 I2C_CMD_READ ioctl 参数*/
struct i2c_data_s
{
    uint32_t rv_id;         // I2C控制器/总线编号
    uint8_t *tx_data;       // 发送给设备的数据（地址+偏移）
    uint32_t tx_data_len;   // 发送数据的长度
    uint8_t *read_data;     // 从设备读取的数据缓冲区
    uint32_t read_data_len; // 期望读取的数据长度
    uint32_t timeout_cnt;   // 超时时间（毫秒）
    uint32_t re_cnt;        // 重试次数
};

/* 写操作传输参数结构体 I2C_CMD_WRITE ioctl 参数*/
struct i2c_write_data_s
{
    uint32_t rv_id;         // I2C控制器/总线编号
    uint8_t *tx_data;       // 发送给设备的数据（地址+偏移）
    uint32_t tx_data_len;   // 发送数据的长度
    uint32_t timeout_cnt;   // 超时时间（毫秒）
};

#endif